//定义一个JS控制器 AMD require.js 模块化插件
//1、需要引入插件
//2、该控制器模块的业务逻辑
define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form){
    // 定义一个控制器
    var Controller = {
        index:function(){
            
            // 初始化表格参数
            // 配置整个表格中增删改查请求控制器的地址，用的ajax情求
            Table.api.init({
                extend:{
                    index_url:'business/privatesea/index',//列表查询的控制器地址
                    add_url:'business/privatesea/add',
                    edit_url:'business/privatesea/edit',
                    del_url:'business/privatesea/del',
                    recycle_url:'business/privatesea/recycle',
                    businessinfo_url:'business/businessinfo/index',
                    table:'business'//表
                }
            });

            // 获取view视图中的dom元素
            var table = $("#table")

            // 渲染表格
            // $.ajax
            table.bootstrapTable({
                url:$.fn.bootstrapTable.defaults.extend.index_url, //请求地址
                toolbar:".toolbar", //工具栏
                pk:'id',//默认主键字段名
                sortOrder:'desc',//排序的方式
                // 渲染的数据部分
                columns:[
                    // 渲染的字段部分
                    {checkbox:true},
                    // operate通用搜索的操作符，sortable切换排序
                    {field:'id',title:'ID',operate:false,sortable:true},
                    {field:'nickname',title:__('BusinessNickname'),operate:'LIKE'},
                    {field:'mobile',title:__('Mobile'),operate:'LIKE'},
                    {field:'email',title:__('Email'),operate:'LIKE'},

                    {field:'source.name',title:__('SourceName'),operate:'LIKE'},
                    {
                        field:'gender',
                        title:__('Gender'),
                        operate:false,
                        searchList: {0: __('Secret'), 1: __('男'),2:__('女')}, 
                        formatter: Table.api.formatter.normal
                    },
                    {
                        field:'deal',
                        title:__('Deal'),
                        operate: 'FIND_IN_SET',
                        searchList: { 1: __('MakeDeal'), 0: __('NoDeal') },
                        formatter: Table.api.formatter.normal  
                    },
                    {
                        field:'auth',
                        title:__('Auth'),
                        operate: 'FIND_IN_SET',
                        searchList: { 1: __('MakeAuth'), 0: __('NoAuth') } ,
                        formatter: Table.api.formatter.normal
                    },
                    
                    // 最后一排操作按钮
                    {
                        field:'operate',
                        title:__('Operate'),
                        table:table,
                        events:Table.api.events.operate,
                        buttons: [
                            {
                                url:'business/privatesea/recycle', //请求地址
                                name: 'recycle',
                                title: __('Recycle'),
                                classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                icon: 'fa fa-recycle',
                                extend:"data-toggle='tooltip'",
                                confirm: '确定要回收吗',
                                success: function (data, ret) {
                                    $(".btn-refresh").trigger("click");
                                },
                                error: function (err) {
                                    console.log(err);
                                }
                            },
                            {
                                url: 'business/businessinfo/index?ids={id}', //请求地址
                                name: 'info',
                                title: function(data){
                                    return `${data.nickname}--客户详情`
                                },
                                classname: 'btn btn-xs btn-primary btn-dialog',
                                icon: 'fa fa-th-list',
                                
                                extend: 'data-toggle=\'tooltip\' data-area= \'["80%", "95%"]\'', //重点是这一句
                            }
                        ],
                        formatter:Table.api.formatter.operate
                    }
                ]
            })

             // 回收，确认框的方法
             $('.btn-recycle').on('click', function () {
                let ids = Table.api.selectedids(table);
                ids = ids.toString()
                layer.confirm('确定要回收吗?', { title: '回收', btn: ['是', '否'] },
                    function (index) {
                        
                        $.post("business/privatesea/recycle", { ids: ids, action: 'success', reply: '' }, function (response) {
                            if (response.code == 1) {
                                Toastr.success(response.msg)
                                $(".btn-refresh").trigger('click');
                            } else {
                                Toastr.error(response.msg)
                            }
                        }, 'json');

                        layer.close(index);
                    }
                );

            });
            // 为表格绑定事件
            Table.api.bindevent(table);


        },
        add:function(){

            // 选中地区事件处理
            $('#region').on('cp:updated',function(){
                var citypicker = $(this).data("citypicker");
                var code = citypicker.getCode("district") || citypicker.getCode("city") || citypicker.getCode("province");
                $("#region-code").val(code);
            })
            //给控制器绑定通用事件
            
            Controller.api.bindevent()
        },
        edit:function(){
            // 选中地区事件处理
            $('#region').on('cp:updated',function(){
                var citypicker = $(this).data("citypicker");
                var code = citypicker.getCode("district") || citypicker.getCode("city") || citypicker.getCode("province");
                $("#region-code").val(code);
            })
            Controller.api.bindevent();
        },
        del:function(){
            Controller.api.bindevent();
        },
        api: {
            //JS模块化的全局方法
            bindevent: function () 
            {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    }
    //模块返回值
    return Controller
})